﻿/********************************************************************************/
/* 	player.d																	*/
/*------------------------------------------------------------------------------*/
/*	製作		（　゜ワ゜）ノ　／　松久貫太									*/
/*	製作開始　	2007/05/11														*/
/*	MAIL		omega@personal.email.ne.jp										*/
/*	URL		http://nagoya.cool.ne.jp/o_mega										*/
/*																				*/
/*	このソースは「やらわかライセンス」の元で配布されています。					*/
/*-更新履歴---------------------------------------------------------------------*/
/*	2007/--/--																	*/
/*-その他-----------------------------------------------------------------------*/
/*	なし																		*/
/********************************************************************************/

/* ライブラリのインポート *******************************************************/
private import hell2;		// dHell2ライブラリ
private import actor;
private import vector;
private import screen;
private import keypad;
private import state;
private import shot;
private import effect;
private import enemy;

private import std.string;
private import std.math;
private import std.math2;
private import std.stream;

private import defines;

/**/

class Player : Actor
{
private:
	static const float NORMAL_MOVE_SPEED = 0.15;
	static const float HIT_SIZE = 0.8;
	static const float MARKER_APPEAR = 5.0;
	static const float SPEED_MAX = 10.0;
	static const float SPEED_RATIO = 0.08;
	
	static const float SHOT_SPEED = 1.0;
	static const float GRAPHICS_SIZE = 0.7;
	static const float AUTOROTATE_SPEED = 0.05f;
	
public:
	int timer;
	double direction;
	Keypad keypad;
	
	Vec3 marker;
	Vec3 cursor;
	Vec3 dashSpeed;
	int marker_timer;
	
	enum STATE
	{
		NORMAL,
		DASH,
	}
	STATE state;
	
	GameMain gamemain;
	
	this(GameMain gm)
	{
		gamemain = gm;
		position = new Vec3(0,0, CHAR_Z_SURFACE );
		marker = new Vec3(0,0, CHAR_Z_SURFACE );
		cursor = new Vec3(0,0, CHAR_Z_SURFACE );
		timer = 0;
		keypad = new Keypad(0);
		
		direction = -3.14159 / 2;
		state = STATE.NORMAL;
		marker_timer = 0;
		
		exist = true;
	}
	
	void move()
	{
		if(!exist) return;
		
		timer++;
		float vx = 0,vy = 0;
		if( keypad.isPressDown() ) vy = 1;
		if( keypad.isPressUp() ) vy = -1;
		if( keypad.isPressLeft() ) vx = -1;
		if( keypad.isPressRight() ) vx = 1;
		position += new Vec3(vx,vy,0) * NORMAL_MOVE_SPEED;
		checkCollisionToEnemy();
		
		directToNearestEnemy();
		// 弾を出す
		
		if(timer % 2 == 0 || gamemain.shotactor.countType(SHOTTYPE.NORMAL) < 4)
		//	&& gamemain.shotactor.countType(SHOTTYPE.NORMAL) < 13)
		{
			if(gamemain.shotactor.countType(SHOTTYPE.NORMAL) < 8)
			{
				double r = direction + 3.141592 / 2;
				gamemain.shotactor.fire(
					new Vec3(position.x + cos(r) * 0.7 , position.y + sin(r) * 0.7 , 0) ,
					new Vec3(SHOT_SPEED * cos(direction) , SHOT_SPEED * sin(direction) ,0) ,
					SHOTTYPE.NORMAL
				);
				r = direction - 3.141592 / 2;
				gamemain.shotactor.fire(
					new Vec3(position.x + cos(r) * 0.7 , position.y + sin(r) * 0.7 , 0) ,
					new Vec3(SHOT_SPEED * cos(direction) , SHOT_SPEED * sin(direction) ,0) ,
					SHOTTYPE.NORMAL
				);
			}
			else
			{
				gamemain.shotactor.fire(
					position ,
					new Vec3(SHOT_SPEED * cos(direction) , SHOT_SPEED * sin(direction) ,0) ,
					SHOTTYPE.NORMAL
				);
			}
			/*
				float diff = (timer % 20) / 20.0f * 3.14159 / 4;
				gamemain.shotactor.fire(
					position ,
					new Vec3(SHOT_SPEED * cos(direction + diff) , SHOT_SPEED * sin(direction + diff) ,0) ,
					SHOTTYPE.NORMAL
				);
				gamemain.shotactor.fire(
					position ,
					new Vec3(SHOT_SPEED * cos(direction - diff) , SHOT_SPEED * sin(direction - diff) ,0) ,
					SHOTTYPE.NORMAL
				);
			*/
		}
	}
	
	void checkCollisionToEnemy()
	{
		Enemy e = gamemain.enemyactor.getNearestEnemy(position);
		
		if(e){
			if( e.position.getDist(position) < 0.5 + e.getSize())
			{
				// Destory
				exist = false;
				
				for(int t=0;t<64;t++)
				{
					gamemain.addEffect(position , position , EFFECTTYPE.TRIANGLE);
				}
			}
		}
	}
	
	void directToNearestEnemy(){
		Enemy e = gamemain.enemyactor.getNearestEnemy(position);
		static const double PI = 3.141592;
		double r = -PI / 2;
		Vec3 target = position;
		
		if(e){
			r = (e.position - position).atan2D();
			target = e.position;
			while( std.math.abs(direction - r) > PI ){
				direction += ( direction < r ) ? ( PI*2 ) : ( -PI*2 );
			}
		}
		/*
		if( std.math.abs( direction - r ) < AUTOROTATE_SPEED ){
			direction = r;
		}else{
			if( direction > r ){
				direction -= AUTOROTATE_SPEED;
			}else{
				direction += AUTOROTATE_SPEED;
			}
		}
		*/
		direction = direction * 0.9f + r * 0.1f;
		cursor = cursor * 0.9f + target * (1.0f - 0.9f);
	}
	
	void drawPlayer()
	{
		if(!exist) return;
		// プレーヤー
		for(int t=0;t<60;t+=20){
			glPushMatrix();
			{
				Hell_setAlpha(HELL_ALPHA_ADD);
				glTranslatef(position.x , position.y , position.z);
				glRotatef(0,0,direction / 3.14159 * 180 + 90);
				double sz = GRAPHICS_SIZE * ( ((timer + t) % 60) / 60.0 * 15.0 + 2.0 );
				glScalef(sz,sz,sz);
				glColor4f(0.1 , 0.6 , 1.0 , (50 - (timer + t) % 60) / 60.0);
				drawBillBoard("player", 32.0 * ((timer/10) % 6)  , 0 , 32.0 , 48.0);
			}
			glPopMatrix();
		}
		
		glPushMatrix();
		{
			Hell_setAlpha(HELL_ALPHA_NORMAL);
			glTranslatef(position.x , position.y , position.z);
			glRotatef(0,0,direction / 3.14159 * 180 + 90);
			double sz = GRAPHICS_SIZE * 3;
			glScalef(sz,sz,sz);
			glColor4f(1,1,1,1);
			drawBillBoard("player", 32.0 * ((timer/10) % 6)  , 0 , 32.0 , 48.0);
		}
		glPopMatrix();
		
		// ロックオンカーソル
		glPushMatrix();
		{
			Hell_setAlpha(HELL_ALPHA_ADD);
			glTranslatef(cursor.x , cursor.y , cursor.z);
			glRotatef(0,0,-timer * 1.5);
			double sz = (timer % 60) / 60.0 * 4 + 5;
			glScalef(sz,sz,sz);
			glColor4f(0.1 , 0.6 , 1.0 , (30 - timer % 60) / 120.0);
			drawBillBoard("player" , 416 , 320 , 96 , 96);
		}
		glPopMatrix();
		glPushMatrix();
		{
			Hell_setAlpha(HELL_ALPHA_NORMAL);
			glTranslatef(cursor.x , cursor.y , cursor.z);
			glRotatef(0,0,timer * 1.5);
			glScalef(5,5,5);
			glColor4f(1,1,1,1);
			//glColor4f(timer / 30.0 , timer / 30.0 , timer / 15.0 , 0.25);
			drawBillBoard("player" , 416 , 320 , 96 , 96);
		}
		glPopMatrix();
	}
	void drawStatus()
	{
		//Hell_drawFont(std.string.toString(direction),320 ,320 ,0.5 );
	}
}